home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / doc / example2.c < prev    next >
C/C++ Source or Header  |  1993-09-01  |  4KB  |  132 lines

  1. /*
  2.  * example2.c: oscillating mesh with FORMS control panel
  3.  *
  4.  * This example module is distributed with the geomview manual.
  5.  * If you are not reading this in the manual, see the "External
  6.  * Modules" chapter of the manual for an explanation.
  7.  *
  8.  * This module creates an oscillating mesh and has a FORMS control
  9.  * panel that lets you change the speed of the oscillation with a
  10.  * slider.
  11.  */
  12.  
  13. #include <math.h>
  14. #include <stdio.h>
  15. #include <sys/time.h>           /* for struct timeval below */
  16.  
  17. #include "forms.h"              /* for FORMS library */
  18.  
  19. FL_FORM *OurForm;
  20. FL_OBJECT *VelocitySlider;
  21. float dt;
  22.  
  23. /* F is the function that we plot
  24.  */
  25. float F(x,y,t)
  26.      float x,y,t;
  27. {
  28.   float r = sqrt(x*x+y*y);
  29.   return(sin(r + t)*sqrt(r));
  30. }
  31.  
  32. /* SetVelocity is the slider callback procedure; FORMS calls this
  33.  * when the user moves the slider bar.
  34.  */
  35. void SetVelocity(FL_OBJECT *obj, long val)
  36. {
  37.   dt = fl_get_slider_value(VelocitySlider);
  38. }
  39.  
  40. /* Quit is the "Quit" button callback procedure; FORMS calls this
  41.  * when the user clicks the "Quit" button.
  42.  */
  43. void Quit(FL_OBJECT *obj, long val)
  44. {
  45.   exit(0);
  46. }
  47.  
  48. /* create_form_OurForm() creates the FORMS panel by calling a bunch of
  49.  * procedures in the FORMS library.  This code was generated
  50.  * automatically by the FORMS designer program; normally this code
  51.  * would be in a separate file which you would not edit by hand.  For
  52.  * simplicity of this example, however, we include this code here.
  53.  */
  54. create_form_OurForm()
  55. {
  56.   FL_OBJECT *obj;
  57.   FL_FORM *form;
  58.   OurForm = form = fl_bgn_form(FL_NO_BOX,380.0,120.0);
  59.   obj = fl_add_box(FL_UP_BOX,0.0,0.0,380.0,120.0,"");
  60.   VelocitySlider = obj = fl_add_valslider(FL_HOR_SLIDER,20.0,30.0,
  61.                                           340.0,40.0,"Velocity");
  62.     fl_set_object_lsize(obj,FL_LARGE_FONT);
  63.     fl_set_object_align(obj,FL_ALIGN_TOP);
  64.     fl_set_call_back(obj,SetVelocity,0);
  65.   obj = fl_add_button(FL_NORMAL_BUTTON,290.0,75.0,70.0,35.0,"Quit");
  66.     fl_set_object_lsize(obj,FL_LARGE_FONT);
  67.     fl_set_call_back(obj,Quit,0);
  68.   fl_end_form();
  69. }
  70.  
  71. main(argc, argv)        
  72.      char **argv;
  73. {
  74.   int xdim, ydim;
  75.   float xmin, xmax, ymin, ymax, dx, dy, t;
  76.   int fdmask;
  77.   static struct timeval timeout = {0, 200000};
  78.  
  79.   xmin = ymin = -5;             /* Set x and y            */
  80.   xmax = ymax = 5;              /*    plot ranges         */
  81.   xdim = ydim = 24;             /* Set x and y resolution */
  82.   dt = 0.1;                     /* Time increment is 0.1  */
  83.  
  84.   /* Forms panel setup.
  85.    */
  86.   foreground();
  87.   create_form_OurForm();
  88.   fl_set_slider_bounds(VelocitySlider, 0.0, 1.0);
  89.   fl_set_slider_value(VelocitySlider, dt);
  90.   fl_show_form(OurForm, FL_PLACE_SIZE, TRUE, "Example 2");
  91.  
  92.  
  93.   /* Geomview setup.
  94.    */
  95.   printf("(geometry example { : foo })\n");
  96.   fflush(stdout);
  97.  
  98.   /* Loop until killed.
  99.    */
  100.   for (t=0; ; t+=dt) {
  101.     fdmask = (1 << fileno(stdin)) | (1 << qgetfd());
  102.     select(qgetfd()+1, &fdmask, NULL, NULL, &timeout);
  103.     fl_check_forms();
  104.     UpdateMesh(xmin, xmax, ymin, ymax, xdim, ydim, t);
  105.   }
  106. }
  107.  
  108. /* UpdateMesh sends one mesh iteration to geomview
  109.  */
  110. UpdateMesh(xmin, xmax, ymin, ymax, xdim, ydim, t)
  111.      float xmin, xmax, ymin, ymax, t;
  112.      int xdim, ydim;
  113. {
  114.   int i,j;
  115.   float x,y, dx,dy;
  116.  
  117.   dx = (xmax-xmin)/(xdim-1);
  118.   dy = (ymax-ymin)/(ydim-1);
  119.  
  120.   printf("(read geometry { define foo \n");
  121.   printf("MESH\n");
  122.   printf("%1d %1d\n", xdim, ydim);
  123.   for (j=0, y = ymin; j<ydim; ++j, y += dy) {
  124.     for (i=0, x = xmin; i<xdim; ++i, x += dx) {
  125.       printf("%f %f %f\t", x, y, F(x,y,t));
  126.     }
  127.     printf("\n");
  128.   }
  129.   printf("})\n");
  130.   fflush(stdout);
  131. }
  132.